package com.example.tencentimdemo.im

import com.tencent.imsdk.v2.V2TIMManager
import com.tencent.imsdk.v2.V2TIMMessage
import com.tencent.imsdk.v2.V2TIMSendCallback
import com.tencent.imsdk.v2.V2TIMValueCallback
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

object ImMessageUtils {

    private val messageManager by lazy { V2TIMManager.getMessageManager() }

    /** 发送文本消息 */
    suspend fun sendC2CTextMessage(msg: String, receiveId: String): Boolean {
        return suspendCoroutine<Boolean> {
            ImUtils.imManager.sendC2CTextMessage(
                msg,
                receiveId,
                object : V2TIMValueCallback<V2TIMMessage> {
                    override fun onSuccess(p0: V2TIMMessage?) {
                        it.resume(true)
                    }

                    override fun onError(p0: Int, p1: String?) {
                        it.resume(false)
                    }
                })
        }
    }

    /** 发送群聊文本消息 */
    suspend fun sendGroupTextMessage(msg: String, groupId: String): Boolean {
        return suspendCoroutine {
            ImUtils.imManager.sendGroupTextMessage(
                msg,
                groupId,
                V2TIMMessage.V2TIM_PRIORITY_NORMAL,
                object : V2TIMValueCallback<V2TIMMessage> {
                    override fun onSuccess(p0: V2TIMMessage?) {
                        it.resume(true)
                    }

                    override fun onError(p0: Int, p1: String?) {
                        it.resume(false)
                    }
                })
        }
    }

    /** 单聊自定义消息 */
    suspend fun sendC2CCustomMessage(contentByteArray:ByteArray, receiveId: String): Boolean{
       return suspendCoroutine {
           ImUtils.imManager.sendC2CCustomMessage(contentByteArray,receiveId,object : V2TIMValueCallback<V2TIMMessage> {
               override fun onSuccess(p0: V2TIMMessage?) {
                   it.resume(true)
               }

               override fun onError(p0: Int, p1: String?) {
                   it.resume(false)
               }
           })
       }
    }

    /** 群聊自定义消息 */
    suspend fun sendGroupCustomMessage(contentByteArray:ByteArray, groupId: String): Boolean{
       return suspendCoroutine {
           ImUtils.imManager.sendC2CCustomMessage(contentByteArray,groupId,object : V2TIMValueCallback<V2TIMMessage> {
               override fun onSuccess(p0: V2TIMMessage?) {
                   it.resume(true)
               }

               override fun onError(p0: Int, p1: String?) {
                   it.resume(false)
               }
           })
       }
    }

    /**
     * Im高级接口:消息发送
     * @param msg
     * @param receiveId
     * @param groupId
     * @return
     */
    suspend fun sendImMsg(msg: ImUiMessage, receiveId: String?= null, groupId: String? = null): Boolean {
        val v2TIMMessage = when(msg.message){
            else -> V2TIMMessage()
        }
        return suspendCoroutine {
            messageManager.sendMessage(v2TIMMessage,
                receiveId,
                groupId,
                V2TIMMessage.V2TIM_PRIORITY_NORMAL,
                false,
                null,
                object : V2TIMSendCallback<V2TIMMessage> {
                    override fun onSuccess(p0: V2TIMMessage?) {
                        it.resume(true)
                    }

                    override fun onError(p0: Int, p1: String?) {
                        it.resume(false)
                    }

                    override fun onProgress(p0: Int) {

                    }
                })
        }
    }
}


